<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button onclick="setCookie()">存cookie</button>
  <button onclick="updateCookie()">改cookie</button>
  <button onclick="delCookie()">删cookie</button>
  <button onclick="getCookie()">取cookie</button>
  <script>
    function setCookie () {
      document.cookie = 'name=lisi'
    }

    function updateCookie () {
      // 修改cookie只需要把cookie重新再存一次把原来的覆盖就可以了
      document.cookie = 'name=zhangsan'
    }

    function delCookie () {
      // 把cookie的过期时间改为已经过去了的时间
      var date = new Date()
      date.setDate(date.getDate() - 1)
      document.cookie = `name=0;expires=${date.toUTCString()}`
    }

    function getCookie () {
      console.log(document.cookie)
    }
  </script>
</body>
</html>